<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>大攻略管理</title>
    <#include "../common/header.ftl"/>
    <#include "../common/page.ftl"/>
    <script type="text/javascript" src="/ckeditor/ckeditor.js"></script>
    <script type="text/javascript">
       $(function () {
           // 富文本编辑器
          var ckeditor = CKEDITOR.replace( 'content', {
               filebrowserBrowseUrl: '并没有图片服务器',
               filebrowserUploadUrl: '/images/upload.do'
           });

           // 推荐数据回显
           $('.inputBtn').click(function() {
               $('#editForm')[0].reset();
               $('#editForm img').attr('src','');
               var data = $(this).data('json');
               var id = "";
               if(data){
                   id = data.id;
                   $('#editForm input[name="id"]').val(data.id);
                   $('#editForm input[name="title"]').val(data.title);
                   $('#editForm input[name="releaseTime"]').val(data.releaseTime);
                   $('#editForm select[name="state"]').val(data.state);
                   $('#editForm input[name="sequence"]').val(data.sequence);
                   $('#editForm input[name="coverUrl"]').val(data.coverUrl);
                   $('#editForm img').attr('src',data.coverUrl);
                   var strategyId = data.strategyId;
                    $.get("/strategyDetail/getStrategyById.do",{strategyId:strategyId},function (subData) {
                        $('#strategy').val(subData.id);

                    });
                   $.get('/strategyCatalog/getCatalogByStrategyId.do',{strategyId:strategyId},function (subData) {
                       var html="<option value='-1'>请选择类别</option>";
                       $.each(subData,function (index, value) {
                           html += "<option value='"+ value.id +"'>"+ value.name +"</option>"
                       });
                       $('#catalog').html(html);
                       $('#catalog').val(data.catalogId);
                   });
               }
               $.get('/strategyDetail/getContetByDetailId.do',{detailId:id},function (subData) {
                   ckeditor.setData(subData.content);
                   $('#editModal').modal('show');
               });
           });
           // 图片更换是回显
           $('#editForm input[name="file"]').change(function () {
               // 创建文件读取器对象
               var read = new FileReader();
               // 读取文件内容
               read.readAsDataURL(this.files[0]);
               // 将用户选择的图片渲染到页面
               $(read).load(function () {
                   $('#editForm img').attr('src',read.result);
               });
               // $('#editForm img').attr('src',window.URL.createObjectURL(this.files[0]));
           });
           // 推荐操作
           $('.btn_submit').click(function () {
               $('#content').html(ckeditor.getData());
               // 插件原因,不能在循环中return false,没效果
               var flag = false;
               $('#editForm').ajaxSubmit({
                   beforeSubmit: function (params) {
                       $.each(params,function (index, value) {
                           // 遍历表单中所有的参数,找到type
                           if($(value).attr("name")==="state"){
                               // 其对应的值如果是-1说明用户没有选择推荐类型
                               if($(value).attr("value") == -1){
                                   flag = true;
                               }
                           }
                       })
                       if(flag){
                           $.messager.alert("温馨提示","请选择推荐类型!");
                           return false;
                       }
                   },
                   success: function(data){
                       if(data.success){
                           window.location.reload();
                       }
                   }
               });
           });
           // 二级联动
           $('#strategy').change(function () {
               // 攻略id
               var strategyId = $(this).val();
               if(strategyId == -1){
                   var html="<option  value='-1'>请选择类别</option>";
                   return;
               }
               $.get('/strategyCatalog/getCatalogByStrategyId.do',{strategyId:strategyId},function (data) {
                  var html="<option value='-1'>请选择类别</option>";
                   $.each(data,function (index, value) {
                      html += "<option value='"+ value.id +"'>"+ value.name +"</option>"
                  });
                   $('#catalog').html(html);
               });
           });

       });
    </script>

</head>

<body>
<div class="container">
<#include "../common/top.ftl"/>
    <div class="row">
        <div class="col-sm-3">
		<#assign currentMenu = "strategyDetail" />
				<#include "../common/menu.ftl" />
        </div>
        <div class="col-sm-9">
            <div class="page-header">
                <h3>大攻略管理</h3>
            </div>
            <div class="row">
                <!-- 提交分页的表单 -->
                <form id="searchForm" class="form-inline" method="post" action="/strategyDetail/list.do">
                    <input type="hidden" name="currentPage" id="currentPage" value="1"/>
                    <div class="form-group">
                    </div>
                    <div class="form-group">
                        <label>关键字</label>
                        <input class="form-control" type="text" name="keyword" placeholder="请输入标题"  value="${(qo.keyword)!''}">
                    </div>
                    <div class="form-group">
                        <button id="query" type="button" class="btn btn-success"><i class="icon-search"></i> 查询</button>
                        <button type="button" class="btn btn-success inputBtn"><i class="icon-plus"></i> 添加攻略文章</button>
                    </div>
                </form>
            </div>
            <div class="row">
                <table class="table">
                    <thead>
                    <tr>
                        <th>标题</th>
                        <th>封面</th>
                        <th>发布时间</th>
                        <th>排序</th>
                        <th>攻略类别</th>
                        <th>操作</th>
                    </tr>
                    </thead>
                    <tbody>
					<#list result.list as vo>
                    <tr>
                        <td>${vo.title!}</td>
                        <td><img src="${vo.coverUrl!}" style="width: 60px" alt=""></td>
                        <td>${(vo.releaseTime?string("yyyy-MM-dd"))!}</td>
                        <td>${(vo.sequence)!}</td>
                        <td>${(vo.catalog.name)!}</td>
                        <td>${vo.stateDisplay}</td>
                        <td>
                            <a href="javascript:;" class="btn btn-success inputBtn" data-json='${vo.jsonString!}'>修改</a>
                        </td>
                    </tr>
					</#list>
                    </tbody>
                </table>

                <div style="text-align: center;">
                    <ul id="pagination" class="pagination"></ul>
                </div>
            </div>
        </div>
    </div>
</div>
<div class="modal fade" id="editModal">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title">编辑/添加</h4>
            </div>
            <div class="modal-body">
                <form class="form-horizontal" action="/strategyDetail/saveOrUpdate.do" method="post" id="editForm" >
                    <input type="hidden" value="" name="id">
                    <div class="form-group" id="parentDiv">
                        <label class="col-sm-3 control-label">标题：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="title">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">封面 ：</label>
                        <div class="col-sm-6">
                            <img  style="width: 250px;"/>
                            <input type="file" class="form-control" name="file">
                            <input type="hidden" class="form-control" name="coverUrl">
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">所属攻略 ：</label>
                        <div class="col-sm-6">
                            <select id="strategy" name="strategyId" class="form-control">
                                <option value="-1">请选择攻略</option>
                                    <#list strategies as s>
                                        <option value="${s.id}">${s.title}</option>
                                    </#list>
                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">攻略类别 ：</label>
                        <div class="col-sm-6">
                            <select id="catalog" name="catalog.id" class="form-control">

                            </select>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">是否发布：</label>
                        <div class="col-sm-6">
                            <select name="state" class="form-control">
                                <option value="1">是</option>
                                <option value="0">否</option>
                            </select>
                        </div><br>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label">排序：</label>
                        <div class="col-sm-6">
                            <input type="text" class="form-control" name="sequence">
                        </div>
                    </div>
                    <div class="form-group">
                        <div class="col-sm-12">
                            <textarea name="content.content" id="content" class="ckeditor" rows="10" cols="80">

                            </textarea>
                        </div>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-primary btn_submit">保存</button>
                <button type="button" class="btn btn-danger" data-dismiss="modal">取消</button>
            </div>
        </div>
    </div>
</div>
</body>
</html>